package org.xaviera.convert;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter implements Converter<Date> {

	private DateFormat dateFormat;
	
	
	public DateFormat getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}
	
	public void setDateFormatString(String dateFormatString) {
		this.dateFormat = new SimpleDateFormat(dateFormatString);
	}

	
	@Override
	public Date convert(String value) throws ConversionException {
		try {
			return dateFormat.parse(value);
		}
		catch (ParseException e) {
			throw new ConversionException(e);
		}
	}

	@Override
	public String convert(Date value) {
		return dateFormat.format(value);
	}

}
